//
//  WPCLeftSidebarView.swift
//  Wallpaper.co
//
//  Created by zhou_yuepeng on 2017/9/23.
//  Copyright © 2017年 www.dahuatech.com. All rights reserved.
//

import UIKit

class WPCLeftSidebarView: UIView {
    private var carouselView: JYCarousel?;
    private var categoryView: UICollectionView?;
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    init(frame: CGRect, delegate: UICollectionViewDelegate) {
        super.init(frame: frame);
        weak var weakDelegate = delegate;
        
        self.backgroundColor = UIColor.gray;
        
        /* 轮播 */
        var cFrame:CGRect = self.bounds;
        cFrame.size.height *= 0.3;
        cFrame.size.width *= 0.8;
        carouselView = JYCarousel.init(frame: cFrame, configBlock: {(config)->(JYConfiguration) in
            
            config?.pageContollType = CarouselPageControllType.LeftPageControl;
            config?.interValTime = 3.0;
            config?.pushAnimationType = CarouselPushType.PushDefault;
            
            return config!;
        }, target: weakDelegate as! JYCarouselDelegate!);
        carouselView?.backgroundColor = UIColor.white;
        self.addSubview(carouselView!);
        
        /* collectionView */
        let layout = UICollectionViewFlowLayout();
        layout.minimumLineSpacing = 1;
        layout.minimumInteritemSpacing = 1;
        layout.itemSize = CGSize(width: 80, height: 60);
        layout.footerReferenceSize = CGSize(width: self.bounds.size.width, height: 3);
        
        categoryView = UICollectionView.init(frame: self.bounds, collectionViewLayout: layout);
        categoryView?.delegate = weakDelegate;
        categoryView?.dataSource = weakDelegate as! UICollectionViewDataSource?;
        categoryView?.showsVerticalScrollIndicator = false;
        self.addSubview(categoryView!);
        
        categoryView?.register(WPCLeftSidebarCell.self, forCellWithReuseIdentifier: "WPCLeftSidebarCell");
        categoryView?.register(WPCLeftSideBarCollectionFooterView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "WPCLeftSideBarCollectionFooterView");
        
        /* masonry布局 */
        self.layoutWithMasonry();
    }
    
    func layoutWithMasonry() {
        carouselView!.makeConstraints({ (make) in
            make!.left.equalTo()(self);
            make!.top.equalTo()(self);
            make!.size.equalTo()(self);
            make!.height.equalTo()(self)!.multipliedBy()(0.3);
        })
        
        categoryView?.backgroundColor = UIColor.white.withAlphaComponent(0.8);
        categoryView!.makeConstraints({ (make) in
            make!.left.equalTo()(self);
            make!.top.equalTo()(self.carouselView?.bottom);
            make!.width.equalTo()(self.carouselView);
            make!.bottom.equalTo()(self);
        })
    }
    
    func reloadBanners(banners:NSMutableArray!) {
        carouselView?.start(with: banners);
    }
    
    func reloadCategorys() {
        categoryView?.reloadData();
    }
    
    func dequeueReusableCell(indexPath:IndexPath, withItem:WPCCategoryItem) -> UICollectionViewCell!{
        let cell:WPCLeftSidebarCell = categoryView?.dequeueReusableCell(withReuseIdentifier: "WPCLeftSidebarCell", for: indexPath) as! WPCLeftSidebarCell;
        cell.categoryItem = withItem;
        return cell;
    }
    
    func dequeueReusableView(kind: String, indexPath:IndexPath) -> UICollectionReusableView!{
        var identifier: String?;
        if UICollectionElementKindSectionFooter == kind {
            identifier = "WPCLeftSideBarCollectionFooterView";
        }
        
        return categoryView?.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: identifier!, for: indexPath);
    }
}
